package com.ddw.meedeeuh.presenters;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.ddw.meedeeuh.MeedeeuhApp;
import com.ddw.meedeeuh.shared.events.StartMovieScrapeEvent;
import com.ddw.meedeeuh.shared.events.StartMovieScrapeEventHandler;
import com.ddw.meedeeuh.shared.mvp.PresenterBase;
import com.ddw.meedeeuh.shared.mvp.View;

public class MovieScraperPresenter extends PresenterBase<MovieScraperPresenter.MovieScraperView> {

	public MovieScraperPresenter(MeedeeuhApp app, MovieScraperView view) {
		super(app, view);
	}

	public static interface MovieScraperView extends View {

		public void showView();

		public void hideView();

		public void setProgress(int percentage);

		public void setMessage(String message);

		public void addCloseListener(ActionListener listener);

		public boolean validateClose();

	}

	@Override
	public void onSetup() {
		// Listen for movie scrape requests
		getApp().getEventBus().register(new StartMovieScrapeEventHandler() {

			@Override
			public void onStartMovieScrape(StartMovieScrapeEvent e) {
				getView().setProgress(0);
				getView().setMessage("Starting scrape, please wait...");
				getView().showView();
			}

		});

		// Listen for attempts to close the dialog
		getView().addCloseListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(getView().validateClose()) {
					getView().hideView();
				}
			}

		});
	}

}
